//
//  ChooseAddressView.m
//  NewChooseAddress
//
//  Created by line0 on 13-4-10.
//  Copyright (c) 2013年 shy. All rights reserved.
//

#import "ChooseAddressView.h"
#import "Address_Singleton.h"

static int x;

@implementation ChooseAddressView
@synthesize pickerView;
@synthesize provinceArray;
@synthesize cityArray;
@synthesize countyArray;
@synthesize postcodeArray;
@synthesize provinceStr;
@synthesize cityStr;
@synthesize countryStr;
@synthesize postcodeStr;
@synthesize address;
@synthesize delegate;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.cancelBtn setFrame:self.bounds];
        [self.cancelBtn setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.65]];
//        [self.cancelBtn addTarget:self action:@selector(cancelChooseAddress) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.cancelBtn];
        
        self.pickerView = [[UIPickerView alloc] init];
        [self.pickerView setFrame:CGRectMake(0, self.frame.size.height - 216 - 120, 320, 216)];
        [self.pickerView setDataSource:self];
        [self.pickerView setShowsSelectionIndicator:YES];
        [self.pickerView setDelegate:self];
        [self.pickerView setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:self.pickerView];
        
        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelChooseAddress)];
        UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [spaceItem setWidth:228];
        UIBarButtonItem *confirmItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleBordered target:self action:@selector(confrimChooseAddress)];
        self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.pickerView.frame.origin.y - 0, 320, 44)];
        [self.toolBar setItems:@[cancelItem, spaceItem, confirmItem]];
        [self addSubview:self.toolBar];
        
        [self getAddressData];
    }
    return self;
}

- (void)cancelChooseAddress
{
    [self removeFromSuperview];
}

- (void)confrimChooseAddress
{
    if ([delegate respondsToSelector:@selector(chooseAddress:postcode:)])
    {
        [self.delegate chooseAddress:self.address postcode:self.postcodeStr];
    }
    
    if ([delegate respondsToSelector:@selector(chooseProvince:city:country:postcode:)])
    {
        [self.delegate chooseProvince:self.provinceStr city:self.cityStr country:self.countryStr postcode:self.postcodeStr];
    }
    [self removeFromSuperview];
}

- (void)getAddressData
{
    [[Address_Singleton sharedAddress_Singleton] getAddressData];
    
    self.provinceArray = [Address_Singleton sharedAddress_Singleton].provinceArray;
    self.cityArray     = [[Address_Singleton sharedAddress_Singleton].cityArray objectAtIndex:0];
    self.countyArray   = [[[Address_Singleton sharedAddress_Singleton].countyArray objectAtIndex:0] objectAtIndex:0];
    
    self.provinceStr = [self.provinceArray objectAtIndex:0];
    self.cityStr     = [self.cityArray     objectAtIndex:0];
    self.countryStr  = [[self.countyArray  objectAtIndex:0] objectForKey:@"county"];
    self.countryStr  = [self.countryStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    self.postcodeStr = [[self.countyArray  objectAtIndex:0] objectForKey:@"postcode"];
}

#pragma mark - PickerViewDelegate - 代理

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{    
    NSString *str = @"";
    if (component == 0)
    {
        return  [self.provinceArray objectAtIndex:row];
    }
    if (component == 1)
    {
        return  [self.cityArray objectAtIndex:row];
    }
    if (component == 2)
    {
        self.postcodeStr = [[self.countyArray objectAtIndex:row] objectForKey:@"postcode"];        
        return [[self.countyArray objectAtIndex:row] objectForKey:@"county"];
    }
    return str;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)
    {
        self.cityArray = [[Address_Singleton sharedAddress_Singleton].cityArray objectAtIndex:row];
        [self.pickerView reloadComponent:1];
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
        
        self.provinceStr = [self.provinceArray objectAtIndex:row];
        self.cityStr     = [self.cityArray objectAtIndex:0];
        
        self.countyArray = [[[Address_Singleton sharedAddress_Singleton].countyArray objectAtIndex:row] objectAtIndex:0];
        [self.pickerView reloadComponent:2];
        [self.pickerView selectRow:0 inComponent:2 animated:YES];
        
        self.countryStr  = [[self.countyArray objectAtIndex:0] objectForKey:@"county"];
        self.countryStr  = [self.countryStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        self.postcodeStr = [[self.countyArray objectAtIndex:0] objectForKey:@"postcode"];
        
        x = row;
    }
    else if (component == 1)
    {
        self.countyArray = [[[Address_Singleton sharedAddress_Singleton].countyArray objectAtIndex:x] objectAtIndex:row];
        [self.pickerView reloadComponent:2];
        [self.pickerView selectRow:0 inComponent:2 animated:YES];
        
        self.countryStr  = [[self.countyArray objectAtIndex:0] objectForKey:@"county"];
        self.countryStr  = [self.countryStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        self.postcodeStr = [[self.countyArray objectAtIndex:0] objectForKey:@"postcode"];
        self.cityStr    = [self.cityArray objectAtIndex:row];
    }
    else if (component == 2)
    {
        self.countryStr  = [[self.countyArray objectAtIndex:row] objectForKey:@"county"];
        self.countryStr  = [self.countryStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        self.postcodeStr = [[self.countyArray objectAtIndex:row] objectForKey:@"postcode"];    
    }
    
    self.address = [NSString stringWithFormat:@"%@%@%@",self.provinceStr,self.cityStr,self.countryStr];
    NSLog(@"address:%@", self.address);
    NSLog(@"postcode:%@", self.postcodeStr);
}

#pragma mark - UIPickerViewDataSource - 数据源

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return [self.provinceArray count];
    }
    if (component == 1)
    {
        return [self.cityArray count];
    }
    if (component == 2)
    {
        return [self.countyArray count];
    }
    return 0;
}


@end
